local GameObjectExtension = CS.Blaze.GameObjectExtension
local blaze = require "blaze"
local app = require "app"
local gamelib = require "club.threecard.gamelib"

local M = gamelib.ui.activity()
local SYNC = { sync = true }

function M:ctor()
    self.res = "club.threecard.gamelib/ui/manual.b:ManualPage"
end

function M:after_route_enter(to, from)
    self:super("after_route_enter", to, from)
    local route = app.router.current()
    if route.meta.page_text then
        self.text.text = app.i18n.text(route.meta.page_text)
    else
        self.text.text = ""
    end
    self:load_content_res(route.meta.page_res)
end

function M:on_set_parent()
    self:super("on_set_parent")
    self.transform:Stretch()
end

function M:load_content_res(res)
    if self.content_res then
        if self.content then
            GameObjectExtension.ReturnToPool(self.content)
        end
        self.app.res.unload(self.content_res)
        self.content_res = nil
        self.content = nil
    end
    if res then
        self.content_res = res
        local ok, result = self.app.res.load(res, SYNC)
        if ok then
            self.content = GameObjectExtension.GetFromPool(result)
            self.content.transform:SetParent(self.scroll_rect.content, false)
        end
    end
end

function M:on_release()
    if self.content_res then
        if self.content then
            GameObjectExtension.ReturnToPool(self.content)
        end
        self.app.res.unload(self.content_res)
        self.content_res = nil
        self.content = nil
    end
end

return M